package com.hycreate.platform.modules.handleinfo;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.hycreate.platform.common.config.Global;
import com.hycreate.platform.common.persistence.Page;
import com.hycreate.platform.common.utils.StringUtils;
import com.hycreate.platform.common.web.BaseController;

/**
 * 处理记录Controller
 * @author wang
 * @version 2017-11-29
 */
@Controller
@RequestMapping(value = "${adminPath}/handleinfo/yhcgHandleinfo")
public class YhcgHandleinfoController extends BaseController {

	@Autowired
	private YhcgHandleinfoService yhcgHandleinfoService;
	
	@ModelAttribute
	public YhcgHandleinfo get(@RequestParam(required=false) String id) {
		YhcgHandleinfo entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = yhcgHandleinfoService.get(id);
		}
		if (entity == null){
			entity = new YhcgHandleinfo();
		}
		return entity;
	}
	
	@RequiresPermissions("handleinfo:yhcgHandleinfo:view")
	@RequestMapping(value = {"list", ""})
	public String list(YhcgHandleinfo yhcgHandleinfo, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<YhcgHandleinfo> page = yhcgHandleinfoService.findPage(new Page<YhcgHandleinfo>(request, response), yhcgHandleinfo); 
		model.addAttribute("page", page);
		return "modules/handleinfo/yhcgHandleinfoList";
	}

	@RequiresPermissions("handleinfo:yhcgHandleinfo:view")
	@RequestMapping(value = "form")
	public String form(YhcgHandleinfo yhcgHandleinfo, Model model) {
		model.addAttribute("yhcgHandleinfo", yhcgHandleinfo);
		return "modules/handleinfo/yhcgHandleinfoForm";
	}

	@RequiresPermissions("handleinfo:yhcgHandleinfo:edit")
	@RequestMapping(value = "save")
	public String save(YhcgHandleinfo yhcgHandleinfo, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, yhcgHandleinfo)){
			return form(yhcgHandleinfo, model);
		}
		yhcgHandleinfoService.save(yhcgHandleinfo);
		addMessage(redirectAttributes, "保存处理记录成功");
		return "redirect:"+Global.getAdminPath()+"/handleinfo/yhcgHandleinfo/?repage";
	}
	
	@RequiresPermissions("handleinfo:yhcgHandleinfo:edit")
	@RequestMapping(value = "delete")
	public String delete(YhcgHandleinfo yhcgHandleinfo, RedirectAttributes redirectAttributes) {
		yhcgHandleinfoService.delete(yhcgHandleinfo);
		addMessage(redirectAttributes, "删除处理记录成功");
		return "redirect:"+Global.getAdminPath()+"/handleinfo/yhcgHandleinfo/?repage";
	}
	
	@RequestMapping(value = "listchu")
	@ResponseBody
	public List<YhcgHandleinfo> listchu(YhcgHandleinfo yhcgHandleinfo, HttpServletRequest request, HttpServletResponse response, Model model) {
		List<YhcgHandleinfo> page = yhcgHandleinfoService.findAllList(yhcgHandleinfo);
		/*Global global = new Global();
		String dir = global.getUserfilesBaseDir();*/
		/*String ip=request.getServerName();
		int	 port=request.getLocalPort();
		String url="http://"+ip+":"+port+"/user";
		for(int i=0;i<page.size();i++){
			page.get(i).setFiletuurl(url+page.get(i).getFiletuurl());
		}*/
		return page;
	}
}